#! /usr/bin/env python
#coding=utf-8


def install(configFile):
    '''
        安装工具的主程序
    '''
    f = open(configFile)

    for elem in f.readlines():
        if len(elem)>0 and elem!='\n':
            toolKey = elem.split('=')[0].replace('\n','').strip()
            toolVal = elem.split('=')[1].replace('\n','').strip()
            
            setupTool = raw_input(u'是否安装'+toolKey+'程序？')
            if setupTool=='Y' or setupTool=='y':
                #初始化工具安装类并调用install安装工具
                getClass(Install.__module__, toolKey).install(toolVal)
            
    f.close()
    
def getClass(module,classStr):
    '''
        通过string映射到class了，并进行初始化
    '''
    import sys
    subClass = '%sInstall' % classStr.capitalize()
    return getattr(sys.modules[module],subClass)()


class Install(object):
    '''
        安装软件的父类
    '''
    def install(self,folder):
        pass

class MavenInstall(object):
    '''
        安装Maven
    '''
    def install(self,folder):
        import os
        installFolder = raw_input(u'输入Maven的安装目录：')
        createFolder(installFolder+'\\maven')#创建文件夹
        copyFolder(folder,os.path.join(installFolder))#拷贝文件
        m2Dir = os.path.expanduser+'\\.m2'
        os.mkdir(m2Dir)
        copy(os.path.join(folder,'LinkCM_settings.xml'),os.path.join(m2Dir,'settings.xml'))

    
class EclipseInstall(object):
    '''
        安装Eclipse
    '''
    def install(self,folder):
        import os
        installFolder = raw_input(u'输入Eclipse的安装目录：')
        createFolder(installFolder+'\\eclipse')#创建文件夹
        copyFolder(folder,os.path.join(installFolder))#拷贝文件
        
    
class EditPlusInstall(object):
    '''
        安装EditPlus
    '''
    def install(self,file):
        import os
        os.system(file) 


def createFolder(folder):
    '''
        创建目录：
            检查folder字符串中的目录是否存在，不存在则按照folder字符串进行文件夹创建
        folder：为一个目录字符串
    '''
    import os
    folder = os.path.normpath(folder)
    list = os.path.splitdrive(folder)
    
    tmpPath = list[0]+'\\'
    for elem in list[1].split('\\'):
        tmpPath = os.path.join(tmpPath,elem)
        if not os.path.exists(tmpPath):
            os.mkdir(tmpPath)
    
    return tmpPath

def copyFolder(srcFolder,destFolder):
    '''
        拷贝文件夹:
            递归拷贝文件夹srcFolder下的文件到,destFolder文件夹中
        srcFolder:源文件夹
        destFolder:目标文件夹
    '''
    import os
    list = os.listdir(srcFolder)
    for elem in list:
        if os.path.isfile(os.path.join(srcFolder,elem)):
            tmpSrcFile = os.path.join(srcFolder,elem)
            tmpDestFile = os.path.join(destFolder,elem)
            import shutil
            shutil.copy(tmpSrcFile,tmpDestFile)
        else:
            tmpSrcFolder = os.path.join(srcFolder,elem)
            tmpDestFolder = os.path.join(destFolder,elem)
            os.mkdir(tmpDestFolder)
            copyFolder(tmpSrcFolder,tmpDestFolder)

def main():
    install('app.properties')

if __name__=='__main__':
    main()
    
    
    
